package Test;

//创建泛型类
class Myarray <T> {
    public Object[] array = new Object[10];//
    public T getPos(int pos) {
        return (T) array[pos];
    }
    public void setVal(int pos,T val) {
        this.array[pos] = val;
    }
}

//写一个泛型类,求一个数组的最大值

//泛型类
class Alg<T extends Comparable<T>> {
    public T findMaxVal(T[] array) {
        T max = array[0];
        for (int i = 0; i < array.length; i++) {
            if(array[i].compareTo(max) > 0) {
                max = array[i];
            }
        }
        return max;
    }
}

class Person implements Comparable<Person>{

    @Override
    public int compareTo(Person o) {
        return 0;
    }
}
//泛型方法
class Alg2 {
    public <T extends Comparable<T>> T findMaxVal(T[] array) {

            T max = array[0];
            for (int i = 0; i < array.length; i++) {
                if(array[i].compareTo(max) > 0) {
                    max = array[i];
                }
            }
            return max;

    }
}
//泛型方法--静态方法
class Alg22 {
    public static <T extends Comparable<T>> T findMaxVal(T[] array) {

        T max = array[0];
        for (int i = 0; i < array.length; i++) {
            if(array[i].compareTo(max) > 0) {
                max = array[i];
            }
        }
        return max;

    }
}

public class demo1 {
    public static void main(String[] args) {
        Integer[] array = {66,799,88};
        Alg22.findMaxVal(array);
    }
    public static void main6(String[] args) {
        Integer[] array = {66,799,88};

        Alg2 alg2 = new Alg2();//实例化对象 不会报错,因为Alg2类是一个普普通通的类
        alg2.<Integer>findMaxVal(array);
        alg2.findMaxVal(array);
    }

    public static void main5(String[] args) {
        Integer[] array = {7,2,3,6,8,1,20};
        Alg<Integer> alg = new Alg<>();
        System.out.println(alg.findMaxVal(array));

        Alg<Person> alg1 = new Alg<>();

    }

    public static void main4(String[] args) {
        //实现一个类,类中包含一个数组成员,使得数组中可以存放任何类型的数据,也可以根据成员方法返回数组中某个下标的值
        Myarray<Integer> myarray = new Myarray<>();
        myarray.setVal(0,1);
        myarray.setVal(1,2);
        int ret = myarray.getPos(0);
        System.out.println(ret);


        Myarray<String> myarray1 = new Myarray<>();
        myarray1.setVal(1,"hello");
        myarray1.setVal(2,"world");
        String ret2 = myarray1.getPos(2);
        System.out.println(ret2);
    }
    public static void main2(String[] args) {
        Integer a = 127;
        Integer b = 127;

        Integer c = 128;
        Integer d = 128;

        System.out.println(a == b);
        System.out.println(c == d);
    }

    public static void main1(String[] args) {
        int i = 10;
        //装箱
        Integer ii = new Integer(i);
        Integer ii2 = Integer.valueOf(i);//手动装箱
        //拆箱
        Integer i3 = new Integer(10);
        int bb = i3;
        int j = ii.intValue();//手动拆箱

        System.out.println(bb);
        System.out.println(j);
        System.out.println(ii);
        System.out.println(ii2);
    }

}
